package com.yz.jdd.module.settings;


import android.content.Intent;
import android.os.Bundle;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.yz.jdd.R;
import com.yz.jdd.base.activity.BaseActivity;
import com.yz.jdd.base.utils.Constants;
import com.yz.jdd.module.main.entity.ShopListItem;
import com.yz.jdd.module.settings.model.Limitation;
import com.yz.jdd.module.settings.model.PayQrCode;
import com.yz.jdd.module.settings.precenter.SelectCodePresenter;
import com.yz.jdd.module.settings.view.SelectCodeView;

import java.util.ArrayList;
import java.util.List;


public class SelectCodeActivity extends BaseActivity implements View.OnClickListener, SelectCodeView {
    ImageView top_back;
    TextView top_title;

    Button confirm_code;
    LinearLayout nodata_layout;
    RecyclerView code_list;
    SmartRefreshLayout srl_main_update;
    TextView shop_name;

    String shopNameStr;
    String shopId;
    //    List<LimitCondition> limitConditions;
    SelectCodePresenter presenter;
    private int limitation;// 是否有限制，1表示有限制，0表示不限制
    private ArrayList<String> limitationList;
//    private LinearLayout ll_all_check;//全选布局
//    private CheckBox select_all_check;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_select_code);
        presenter = new SelectCodePresenter(this);
        initView();

    }

    private void initView() {
        Intent intent = getIntent();
        if (intent != null) {
            shopNameStr = intent.getStringExtra("shopNameStr");
            shopId = intent.getStringExtra("shopId");
//            limitConditions= (List<LimitCondition>) intent.getSerializableExtra("limitConditions");
            limitation = intent.getIntExtra("limitation", 0);
            ArrayList<Limitation> arrayList = (ArrayList<Limitation>) intent.getSerializableExtra("limitationList");
            if (arrayList != null && arrayList.size() > 0) {
                limitationList = new ArrayList<>();
                for (Limitation limitation : arrayList) {
                    limitationList.add(limitation.getQrCode());
                }
            }
//            presenter.getQrCodeList(shopId);
            presenter.doQueryQrCodesByShopIdList(shopId);
        }

        top_back = findViewById(R.id.back_nav);
        top_back.setVisibility(View.VISIBLE);
        top_back.setOnClickListener(this);

        top_title = findViewById(R.id.title_nav);
        top_title.setText("选择收款码");
        top_title.setVisibility(View.VISIBLE);

        confirm_code = findViewById(R.id.confirm_code);
        confirm_code.setOnClickListener(this);

        nodata_layout = findViewById(R.id.nodata_layout);
        code_list = findViewById(R.id.code_list);
        code_list.setLayoutManager(new LinearLayoutManager(this));

//        ll_all_check = findViewById(R.id.ll_all_check);
//        select_all_check = findViewById(R.id.select_all_check);

        srl_main_update = findViewById(R.id.srl_main_update);
        shop_name = findViewById(R.id.shop_name);
        if (!TextUtils.isEmpty(shopNameStr)) {
            shop_name.setText(shopNameStr);
        }
    }

    @Override
    public void onClick(View view) {
        if (view.getId() == R.id.back_nav) {
            finish();
        } else if (view.getId() == R.id.confirm_code) {
//            presenter.doAppLimitSetting(shopId,shopNameStr);
            presenter.doSwitchLimitationSetting();
        }
    }

    @Override
    public void showLoading() {
        super.showLoading("加载中...");
    }

    @Override
    public void hidenLoading() {
        super.dismissLoading();
    }

    @Override
    public void finishBackResult(List<String> qrCodeList) {

//        Intent intent = new Intent();
//        intent.putExtra("selectCodeList", (Serializable) qrCodeList);
//        setResult(Constants.SelectCodeResultCode, intent);
//        finish();
        Intent intent = new Intent();
        ShopListItem.ResultObjectBean.PageDataBean.ShopsBean shopsBean = new ShopListItem.ResultObjectBean.PageDataBean.ShopsBean();
        shopsBean.setShopId(shopId);
        shopsBean.setShopName(shopNameStr);
        Bundle bundle = new Bundle();
        intent.putExtra("shopsBean", shopsBean);
        intent.putExtras(bundle);
        setResult(Constants.SelectCodeResultCode, intent);
        finish();
    }

    @Override
    public void showErroMsg(String msg) {
        showToastDialog(msg);
    }

    @Override
    public void showView(final List<PayQrCode> payQrCodes) {
        if (payQrCodes != null && payQrCodes.size() > 0) {

            if (limitationList != null && limitationList.size() > 0) {
                if (limitationList.size() > 0) {
                    confirm_code.setText("确定(" + limitationList.size() + ")");
                } else {
                    confirm_code.setText("确定");
                }

                if (limitation == 0) {
                    //全选
//                    for (int i = 0; i < payQrCodes.size(); i++) {
//                        payQrCodes.get(i).setSelected(true);
//                    }
//                    select_all_check.setChecked(true);
//                    confirm_code.setText("确定(" + payQrCodes.size() + ")");
                } else {
                    if (limitationList != null && limitationList.size() > 0) {
                        for (String limitCondition : limitationList) {
//                Log.e("lkf",""+limitCondition.getQrCode());
                            for (int i = 0; i < payQrCodes.size(); i++) {
                                if (limitCondition.equals(payQrCodes.get(i).getQrcodeId())) {
//                        Log.e("lkf",""+payQrCodes.get(i).getQrcodeId());
                                    payQrCodes.get(i).setSelected(true);
                                }
                            }
                        }
                    }
                }
            }
//            ll_all_check.setOnClickListener(new View.OnClickListener() {
//                @Override
//                public void onClick(View v) {
//                    select_all_check.performClick();
//                }
//            });

            final SelectCodeAdapter selectCodeAdapter = new SelectCodeAdapter(this, payQrCodes);
            code_list.setAdapter(selectCodeAdapter);
            code_list.setVisibility(View.VISIBLE);
            nodata_layout.setVisibility(View.GONE);

//            select_all_check.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
//                @Override
//                public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
//                    for (int i = 0; i < payQrCodes.size(); i++) {
//                            payQrCodes.get(i).setSelected(isChecked);
//                    }
//                    selectCodeAdapter.notifyDataSetChanged();
//                    setSelectSize(payQrCodes);
//                }
//            });
        } else {
            code_list.setVisibility(View.GONE);
//            ll_all_check.setVisibility(View.GONE);
            confirm_code.setVisibility(View.GONE);
            nodata_layout.setVisibility(View.VISIBLE);
        }

    }

    public void setSelectSize(List<PayQrCode> payQrCodes) {
        List<String> qrCodeList = new ArrayList<>();
        if (payQrCodes != null) {
            for (PayQrCode payQrCode : payQrCodes) {
                if (payQrCode.isSelected()) {
                    qrCodeList.add(payQrCode.getQrcodeId());
                }
            }
        }
        if (qrCodeList.size() > 0) {
            confirm_code.setText("确定(" + qrCodeList.size() + ")");
        } else {
            confirm_code.setText("确定");
        }

    }
}
